package cn.zcib.tms.web;

import cn.zcib.tms.dao.*;
import cn.zcib.tms.model.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ExhibitionController {

    @Value("${pageSize}")
    private Integer pageSize;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private SourceMapper sourceMapper;

    @Autowired
    private ServiceMapper serviceMapper;

    @Autowired
    private OrderMapper orderMapper;


    @GetMapping({"/", "/index" ,"/source"})
    public String index(@RequestParam(defaultValue = "0") Integer type,@RequestParam(defaultValue = "") String key,@RequestParam(defaultValue = "1") Integer currentPage,Model model) {
        PageHelper.startPage(currentPage,16);
        List<Source> list = sourceMapper.findAllSource(type,key);


        PageInfo<Source> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("type",type);
        model.addAttribute("key",key);
        System.out.println(pageInfo);
        return "index";
    }


    /**
     * 显示服务设施
     *
     * */
    @GetMapping({"/service"})
    public String showService(@RequestParam(defaultValue = "0") Integer type,@RequestParam(defaultValue = "") String key,@RequestParam(defaultValue = "1") Integer currentPage,Model model) {
        PageHelper.startPage(currentPage,9);
        List<Service> list = serviceMapper.findAllService(type,key);


        PageInfo<Service> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("type",type);
        model.addAttribute("key",key);
        System.out.println(pageInfo);
        return "service";

    }

    /**
     * 显示商品信息
     *
     * */
    @GetMapping({"/shop"})
    public String showShop(@RequestParam(defaultValue = "0") Integer type,@RequestParam(defaultValue = "") String key,@RequestParam(defaultValue = "1") Integer curragePage,Model model) {
        PageHelper.startPage(curragePage,8);
        List<Shop> list = shopMapper.shop(type,key);

        System.out.println(curragePage);

        PageInfo<Shop> pageInfo = new PageInfo<Shop>(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("type",type);
        model.addAttribute("key",key);
        System.out.println(pageInfo);
        return "shop";
    }


    /**
     * 显示商品详细信息
     *
     * */
    @GetMapping({"/shop/detail"})
    public String showShopInfo(@RequestParam(defaultValue = "0") String id, Model model) {
        Shop shop = shopMapper.findShopById(id);
        List<Route> routes = routeMapper.findRouteByTypeId("0",id);
        shop.setRoutes(routes);
        System.out.println(shop);
        model.addAttribute("category","shop");
        model.addAttribute("info",shop);
        return "detailinfo";
    }

    /**
     * 显示服务设施信息
     *
     * */
    @GetMapping("/service/detail")
    public String showServiceInfo(@RequestParam(defaultValue = "0") String id, Model model) {
        Service service = serviceMapper.findServiceById(id);
        List<Route> routes = routeMapper.findRouteByTypeId("1",id);
        service.setRoutes(routes);
        System.out.println(service);
        model.addAttribute("category","service");
        model.addAttribute("info",service);
        return "detailinfo";
    }

    /**
     * 显示旅游详细信息
     *
     * */
    @GetMapping("/source/detail")
    public String showSourceInfo(@RequestParam(defaultValue = "0") String id, Model model) {
        Source source = sourceMapper.findSourceById(id);
        model.addAttribute("category","source");
        model.addAttribute("info",source);
        return "detailinfo";
    }

    /**
     * 预订客房信息
     * */

    @RequestMapping("/book")
    public String bookRoom(Orders orders, String o_time , Model model, HttpSession session) throws ParseException {
        String startTime = o_time.substring(0,10);
        String endTime   = o_time.substring(12,23);
        System.out.println(startTime + endTime);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ss");
        Date startDate = simpleDateFormat.parse(startTime);
        Date endDate = simpleDateFormat.parse(endTime);
        orders.setO_start_time(startDate);
        orders.setO_end_time(endDate);
        orders.setO_create_time(new Date());
        orders.setUser((User)session.getAttribute("user"));
        orderMapper.addOrder(orders);
        return  "redirect:/order";
    }

    /**
     * 查看用户的订单信息*/

    @GetMapping("/order")
    public String findAllOrderByUid(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");

        List<Orders> orders = orderMapper.findAllOrderByUid(user.getU_id());
        for (Orders order : orders) {
            Service service = serviceMapper.findServiceById(order.getO_se_id().toString());
            order.setService(service);
        }
        model.addAttribute("orders",orders);
        System.out.println(orders);
        return "myorder";
    }



}
